package light.mvc.controller.wxn;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.pageModel.base.Grid;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.wxn.CumulateUserVO;
import light.mvc.service.wxn.AccessTokenServiceI;
import light.mvc.service.wxn.CumulateUserServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/cumulateUser")
public class CumulateUserController extends BaseController {

	@Autowired
	private CumulateUserServiceI cumulateUserService;
	
	@Autowired
	private AccessTokenServiceI accessTokenService;

	@RequestMapping("/manager")
	public String manager() {
		return "/wxn/cumulateUser";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(CumulateUserVO cumulateUser, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(cumulateUserService.dataGrid(cumulateUser, ph));
		grid.setTotal(cumulateUserService.count(cumulateUser, ph));
		return grid;
	}

	@RequestMapping("/viewPage")
	public String viewPage(HttpServletRequest request, Long id) {
		CumulateUserVO r = cumulateUserService.get(id);
		request.setAttribute("cumulateUser", r);
		return "/wxn/cumulateUserView";
	}

	@RequestMapping("/addPage")
	public String addPage() {
		return "/wxn/cumulateUserAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(CumulateUserVO cumulateUser) {
		Json j = new Json();
		try {
			cumulateUserService.add(cumulateUser);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Long id) {
		Json j = new Json();
		try {
			cumulateUserService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/get")
	@ResponseBody
	public CumulateUserVO get(Long id) {
		return cumulateUserService.get(id);
	}

	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Long id) {
		CumulateUserVO r = cumulateUserService.get(id);
		request.setAttribute("cumulateUser", r);
		return "/wxn/cumulateUserEdit";
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(CumulateUserVO cumulateUser) {
		Json j = new Json();
		try {
			cumulateUserService.edit(cumulateUser);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/getCumulateUser")
	@ResponseBody
	public Json getAnalysisUser(String appid) {
		return accessTokenService.addCumulateUser(appid);
	}
}